package com.xiyuan.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import com.xiyuan.dao.FamilyMapper;
import com.xiyuan.dao.HuiyuanMapper;
import com.xiyuan.entity.Family;
import com.xiyuan.entity.Huiyuan;
import com.xiyuan.service.TxService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author：wujinxing
 * @since：2016-2-27 下午6:24:52
 * @version: 1.0
 * @function:
 */
@Controller
public class HuiyuanCURDAction {

	@Autowired
	private HuiyuanMapper huiyuanMapper;
	@Autowired
	private FamilyMapper familyMapper;
	
	@Autowired
	private TxService txService;

	// 添加会员
	@RequestMapping(value = "/huiyuan_add.jsp", method = RequestMethod.GET)
	public String add(HttpServletRequest request) {

		return "/huiyuan_add";
	}

	// 添加会员提交
	@RequestMapping(value = "/huiyuan_add.jsp", method = RequestMethod.POST)
	public String addPost(HttpServletRequest request,
			@ModelAttribute("huiyuanForm") Huiyuan huiyuan,
			@RequestParam("thebirthday") String thebirthday,
			@RequestParam("lastday") String lastday) throws ParseException {
		Date now = new Date();

		if (thebirthday != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
			huiyuan.setBirthday(dateFormat.parse(thebirthday));
		}
		if (lastday != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
			huiyuan.setDeadline(dateFormat.parse(lastday));
		}
		huiyuan.setCreatetime(now);
		huiyuanMapper.insertSelective(huiyuan);
		int hyid = huiyuan.getId();// 返回插入的id
		ArrayList<Family> fs = huiyuan.getFamilys();
		Family family;
		if (fs != null) {
			for (int i = 0; i < fs.size(); i++) {
				family = fs.get(i);
				// 如果家庭参数都为空，则跳过此条不插入
				if (StringUtils.isBlank(family.getGuanxi())
						&& StringUtils.isBlank(family.getZhiye())
						&& StringUtils.isBlank(family.getName()))
					continue;
				family.setHyid(hyid);
				family.setCreatetime(now);
				familyMapper.insertSelective(family);
			}
		}
		return "redirect:/zeou_add.jsp?id=" + hyid;
	}

	// 修改会员
	@RequestMapping(value = "/huiyuan_update", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "id", required = false) String id,
			ModelMap modelMap) {
		if (StringUtils.isBlank(id)) {
			return "500";
		}

		Huiyuan hy = huiyuanMapper.selectByPrimaryKey(Integer.valueOf(id));
		if (hy.getHuodong() != null) {
			String[] huodong = hy.getHuodong().split(",");

			for (int i = 0; i < huodong.length; i++) {
				if (new String("户外拓展").equals(huodong[i]))
					modelMap.put("huodong1", "checked='checked'");
				if (new String("小型室内聚会").equals(huodong[i]))
					modelMap.put("huodong2", "checked='checked'");
				if (new String("大型社交活动").equals(huodong[i]))
					modelMap.put("huodong3", "checked='checked'");
				if (new String("私人定制活动").equals(huodong[i]))
					modelMap.put("huodong4", "checked='checked'");
			}

		}

		modelMap.put("hy", hy);

		return "/huiyuan_update";
	}

	// 修改会员提交
	@RequestMapping(value = "/huiyuan_update", method = RequestMethod.POST)
	public String updatePost(@ModelAttribute("huiyuanForm") Huiyuan huiyuan,
			@RequestParam("thebirthday") String thebirthday,
			@RequestParam("lastday") String lastday,
			HttpServletRequest request) throws ParseException {
		
		
		Date now = new Date();

		if (thebirthday != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
			huiyuan.setBirthday(dateFormat.parse(thebirthday));
		}
		if (lastday != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
			huiyuan.setDeadline(dateFormat.parse(lastday));
		}
		huiyuan.setUpdatetime(now);

		huiyuanMapper.updateByPrimaryKeySelective(huiyuan);

		return "redirect:/huiyuan_detail.jsp?id=" + huiyuan.getId();
	}

	// 删除会员
	@RequestMapping(value = "/huiyuan_delete.jsp")
	public String delete(HttpServletRequest request,
			@RequestParam(value = "id", required = false) String id) {

		if (StringUtils.isBlank(id)) {
			return "500";
		}
		txService.deleteHuiyuan(Integer.valueOf(id));
		
		return "redirect:/huiyuan_list.jsp";
	}
}
